package com.example.jeong.yuejian10.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.jeong.yuejian10.Bean.UserBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Model.UserModel;
import com.example.jeong.yuejian10.R;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,RetrofitListener<UserBean> {
    //声明
    private ImageButton imageBack;
    private EditText userNamer,userPassr,userPass2r,userTelr,userAddressr;
    private Button regBtnr;
    private String str_unamer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();//初始化
        initEvents();//点击事件处理
    }

    private void initViews() {

        userNamer = (EditText) findViewById(R.id.unamer);
        userPassr = (EditText) findViewById(R.id.upassr);
        userTelr = (EditText) findViewById(R.id.utelr);
        userAddressr = (EditText) findViewById(R.id.uadresr);
        userPass2r = (EditText) findViewById(R.id.upass2r);
        regBtnr = (Button) findViewById(R.id.regbtnr);
        imageBack = (ImageButton) findViewById(R.id.image_back);
    }

    private void initEvents() {
        imageBack.setOnClickListener(this);
        regBtnr.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regbtnr:
                //处理注册的点击事件
                str_unamer = userNamer.getText().toString().trim();
                String str_upass = userPassr.getText().toString().trim();
                String str_upass2r = userPass2r.getText().toString().trim();
                final String str_tel = userTelr.getText().toString().trim();
                String str_address = userAddressr.getText().toString().trim();
                String num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。

                //matches():字符串是否在给定的正则表达式匹配
                if(!str_tel.matches(num)){
                    Toast.makeText(RegisterActivity.this, "电话号码不合法", Toast.LENGTH_SHORT).show();
                }


                if (str_unamer.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请填写用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_upass.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请填写密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_upass2r.equals("")) {
                    Toast.makeText(RegisterActivity.this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_tel.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请填写电话", Toast.LENGTH_SHORT).show();
                    return;
                } else if (str_address.equals("")) {
                    Toast.makeText(RegisterActivity.this, "请填写地址", Toast.LENGTH_SHORT).show();
                    return;
                }else {
                    registerBtn();
                }


                break;
            case R.id.image_back:
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                startActivityForResult(intent,1);
                break;
        }
    }

    public void registerBtn(){
        Log.i("registerBtn", "233");
        UserModel userModel = new UserModel();
        userModel.register(str_unamer, userPassr.getText().toString(), userTelr.getText().toString(),
                userAddressr.getText().toString(),this);

    }



    @Override
    public void onSuccess(UserBean userBean) {
        if ("1".equals(userBean.getSuccess())) {
            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
            Bundle bundle = new Bundle();
            bundle.putString("userphone", userTelr.getText().toString().trim());
            Intent intent1 = new Intent();
            intent1.putExtras(bundle);
            setResult(RESULT_OK, intent1);
            finish();
        } else if("3".equals(userBean.getSuccess())){
            Toast.makeText(RegisterActivity.this, "电话号码被占用", Toast.LENGTH_SHORT).show();
        }
        else if("2".equals(userBean.getSuccess())){
            Toast.makeText(RegisterActivity.this, "用户名被占用", Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
//            Intent intent1 = new Intent();
//            intent1.setClass(RegisterActivity.this,LoginActivity.class);
//            startActivityForResult(intent1,1);


        }
    }

    @Override
    public void onFail() {
        Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
    }
}

